<template>
  <div class="center-energy-report">
    <dv-loading v-if="loading"></dv-loading>
    <Wateranalysis
      v-else
      :waterUseData="waterUseData"
      :tableList="tableList"
      :tableTitles="tableTitles"
      :optionsData="optionsData"
      @Checkyear="checkyear"
      @ConfirmQuery="confirmQuery"
      @Selectmonth="selectmonth"
    />
  </div>
</template>
<script>
function getCurrentMonth() {
  var date = new Date();
  var yesterday_milliseconds = date.getTime() - 1000 * 60 * 60 * 24;
  var yesterday = new Date();
  yesterday.setTime(yesterday_milliseconds);

  var strYear = yesterday.getFullYear();
  var strDay = yesterday.getDate();
  var strMonth = yesterday.getMonth() + 1;
  if (strMonth < 10) {
    strMonth = "0" + strMonth;
    strDay = "0" + strDay;
  }
  return strMonth;
}
import Wateranalysis from "../../components/wateranalysis/Wateranalysis";
import {
  searchwaterUseDataConsumption, //环比增长
  searchwaterUseDataStruts, //echarts图
  searchwaterUseDataList, // table
  exportWaterUseDataReport, //导出
} from "../../api/base8090.js";
import { download } from "../../utils/index.js";
export default {
  name: "Centerenergyreport",
  components: {
    Wateranalysis,
  },
  created() {
    this.timer = setInterval(() => {
      if (document.readyState === "complete") {
        this.loading = false;
        window.clearInterval(this.timer);
      }
    }, 1000);
    this.initData();
  },
  data() {
    return {
      loading: true,
      timer: null,
      tableList: [],
      tableTitles: [],
      optionsData: {},
      waterUseData: {},
      queryPiesParmas: {
        stationCode: sessionStorage.getItem("stationCode"),
        year: new Date().getFullYear() + "",
      },
      queryTableParmas: {
        stationCode: sessionStorage.getItem("stationCode"),
        year: new Date().getFullYear() + "",
        month: new Date().getFullYear() + "-" + getCurrentMonth(),
      },
    };
  },
  methods: {
    initData() {
      this.tableList = [];
      this.tableTitles = [];
      this.optionsData = {};
      this.waterUseData = {};
      //环比增长
      searchwaterUseDataConsumption(this.queryPiesParmas).then((res) => {
        if (res.code == 200) {
          this.waterUseData = res.data;
        }
      });
      // table
      searchwaterUseDataList(this.queryTableParmas).then((res) => {
        if (res.code == 200) {
          this.tableTitles = res.data[0];
          let arr = res.data;
          arr.splice(0, 1);
          let item = [];
          arr.forEach((res) => {
            let newObject = {};
            res.forEach((item) => {
              newObject[item.tag] = item.value;
            });
            item.push(newObject);
          });
          this.tableList = item;
        }
      });
      // echarts图
      searchwaterUseDataStruts(this.queryPiesParmas).then((res) => {
        if (res.code == 200) {
          this.optionsData = res.data;
        }
      });
    },
    // 年查询
    checkyear(year) {
      this.queryPiesParmas.year = year;
      this.queryTableParmas.year = year;
      this.initData();
    },
    // 月份选择查询table数据
    selectmonth(month) {
      this.queryTableParmas.month = this.queryTableParmas.year + "-" + month;
      this.tableList = [];
      this.tableTitles = [];
      searchwaterUseDataList(this.queryTableParmas).then((res) => {
        if (res.code == 200) {
          this.tableTitles = res.data[0];
          let arr = res.data;
          arr.splice(0, 1);
          let item = [];
          arr.forEach((res) => {
            let newObject = {};
            res.forEach((item) => {
              newObject[item.tag] = item.value;
            });
            item.push(newObject);
          });
          this.tableList = item;
        }
      });
    },
    // 导出按钮
    confirmQuery() {
      exportWaterUseDataReport(this.queryTableParmas).then((res) => {
        if (res) {
          download(
            res,
            "xls",
            "12号线" + this.queryTableParmas.month + "用水月报表"
          );
          this.$message.success("导出成功");
        } else {
          this.$message.error("导出失败");
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.center-energy-report {
  width: 100%;
  height: 100%;
}
</style>
